package enums;

import java.util.Scanner;

/**
 * 输入
 */
interface Input {

    enum Money implements Input {
        /**
         * 放入钞票
         */
        Y5(5), Y10(10), Y20(20), Y50(50);

        private int amount;

        Money(int amount) {
            this.amount = amount;
        }

        @Override
        public int getAmount() {
            return this.amount;
        }
    }

    enum Item implements Input {
        /**
         * 选择商品
         */
        Cola(5), Water(4), Coffee(25), Soda(6);

        private int amount;

        Item(int amount) {
            this.amount = amount;
        }

        @Override
        public int getAmount() {
            return this.amount;
        }
    }

    enum Operate implements Input {
        Cancel,
        /**
         * 退出
         */
        Stop;

        @Override
        public int getAmount() {
            throw new RuntimeException();
        }
    }

    public int getAmount();
}

class VendingMachine {

    private static int amount = 0;
    static State state = State.Reset;
    private static Input input;

    enum State {
        // 初始化状态
        Reset {
            @Override
            void next() {
                input = input();
                if(input instanceof Input.Money)
                    state = Recharge;
                else if(input instanceof Input.Item)
                    state = Buy;
                else if(input instanceof Input.Operate)
                    state = Stop;
            }
        }, Recharge {
            @Override
            void next() {
                amount += input.getAmount();
                System.out.println("充值成功, " + input.getAmount() + " 元");
                state = Reset;
            }
        }, Buy {
            @Override
            void next() {
                if(amount < input.getAmount()) {
                    System.out.println("余额不足，请放入钞票");
                    state = Reset;
                    return;
                }
                amount -= input.getAmount();
                System.out.println("购买成功，" + input);
                state = Reset;
            }
        }, Stop {
            @Override
            void next() {
                if(amount > 0)
                    System.out.println("退出，退款 " + amount + " 元，请查收");
                System.out.println("感谢您的光临，欢迎下次再来！");
                System.exit(0);
            }
        };

        abstract void next();

        private Scanner scanner = new Scanner(System.in);
        /**
         * 读取输入的指令
         * @return
         */
        Input input() {
            System.out.println("您的余额：" + amount + " 元");
            System.out.println("请输入（0：放入钞票，1：选择商品，2：输入操作）：");
            int i = Integer.parseInt(scanner.nextLine());
            switch(i) {
                case 0:
                    System.out.println("请输入金额：Y5(5), Y10(10), Y20(20), Y50(50)");
                    return Input.Money.valueOf(scanner.nextLine());
                case 1:
                    System.out.println("请选择商品：Cola(5), Water(4), Coffee(25), Soda(6)");
                    return Input.Item.valueOf(scanner.nextLine());
                case 2:
                    System.out.println("请选择操作（Cancel，Stop）");
                    return Input.Operate.valueOf(scanner.nextLine());
            }
            System.out.println("输入错误，清重新输入！");
            return input();
        }
    }
}

/**
 * 售货机
 */
public class VendingMachineDemo {
    public static void main(String[] args) {
        while (true) {
            VendingMachine.state.next();
        }
    }
}
